<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.util.*,util.*,dal.*,model.*,bll.*"%>
<%
	QuotationItemModel obj;
	Object temp = request.getAttribute("quotation_item");
	if (temp != null) {
		try {
			obj = (QuotationItemModel) temp;
		} catch (Exception ex) {
			obj = new QuotationItemModel();
		}
	} else {
		obj = new QuotationItemModel();
	}
	
	int idQuotation = obj.getIdQuotation();
	
	QuotationModel quotation = CacheUtil.getQuotation(idQuotation);
	String quotationName = quotation.getName();
	String quotationUrl = "quotation.jsp?id=" + idQuotation;

	String projectName = CacheUtil.getProjectName(quotation.getIdProject());
	String projectUrl = "project.jsp?id=" + quotation.getIdProject();
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Edit Quotation Item</title>
<link rel="stylesheet" href="css/common.css" />
<link rel="stylesheet" href="css/inputForm.css" />
<link rel="stylesheet" href="css/listForm.css" />
<link rel="stylesheet" href="css/jquery-ui-1.8.14.custom.css" type="text/css" />
<link rel="stylesheet" href="css/hyjack.css" type="text/css" />
<link rel="stylesheet" href="css/datepicker.css" type="text/css" />
<link rel="stylesheet" href="css/themes/ui-lightness/jquery.ui.all.css" />
<link rel="stylesheet" href="css/jquery-ui-timepicker.css" type="text/css" />
<link rel="stylesheet" href="css/jquery-datepicker.css" />
<link rel="stylesheet" href="css/jquery-dialog.css" />

<script language="javascript" type="text/javascript" src="js/jquery-1.6.1.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.hyjack.select.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.ui.core.min.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.ui.widget.min.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.ui.timepicker.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.ui.datepicker.js"></script>
<script language="javascript" type="text/javascript" src="js/datepicker.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.hyjack.js"></script>
<script language="javascript" type="text/javascript" src="js/ajaxQuickInsert.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.ui.tools.min.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.numeric.js"></script>
<script language="javascript" type="text/javascript" src="js/common.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.14.custom.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-timepicker-addon.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(pageLoaded);
function pageLoaded()
{
	turnOnNumbericInput();
	initTooltip();
}

function toggleIsGroupFields()
{
	toggleControls(['tblUnit', 'tblEstimateQuantity', 'tblEstimateUnitCost', 'tblEstimateTotalAmount','tblInQuotation','tblRealQuantity','tblRealUnitCost','tblRealTotalAmount']);
	if(isInQuotation() == true)
		toggleInQuotationFields();
	if(isIsGroup()== true)
	{
		$("#tdContainer").css("padding-left","400px");
		$("#tdContainer").css("padding-right","400px");
		$(".btnSubmit").css("margin-left","-132px");
	}
	else
	{
		$("#tdContainer").css("padding-left","250px");
		$("#tdContainer").css("padding-right","250px");
		$(".btnSubmit").css("margin-left","40px");
	}
	
}
function toggleInQuotationFields()
{
	toggleControls(['tblQuotationQuantity','tblQuotationUnitCost','tblQuotationTotalAmount']);
}
function isInQuotation()
{
	if(document.getElementById("in_quotation").checked==true)
		return true;
	else
		return false;
}
function isIsGroup()
{
	if(document.getElementById("is_group").checked==true)
		return true;
	else
		return false;
}
var requiredInputs = [['display_index','display_index_required'],['name','name_required'],['name','name_required']];
var requiredIsGroupInputs = [['estimate_quantity','estimate_quantity_required'],['estimate_unit_cost','estimate_unit_cost_required'],['estimate_total_amount','estimate_total_amount_required'],['internal_quantity','internal_quantity_required'],['internal_unit_cost','internal_unit_cost_required'],['internal_total_amount','internal_total_amount_required']];
var requiredInQuotationInputs = [['quotation_quantity','quotation_quantity_required'],['quotation_unit_cost','quotation_unit_cost_required'],['quotation_total_amount','quotation_total_amount_required']];
var intInputs = [];
var doubleInputs = [];
var dateInputs = [];
var timeInputs = [];


function checkInputs() {
	var valid = true;

	if (checkRequiredInputs(requiredInputs) == false) {
		valid = false;
	}
	if (checkRequiredIsGroupInputs(requiredIsGroupInputs,'is_group') == false) {
		valid = false;
	}
	if (isIsGroup() == false && checkRequiredInQuotationInputs(requiredInQuotationInputs,'in_quotation') == false) {
		valid = false;
	}
	// 	if (checkIntInputs(intInputs) == false) {
// 		valid = false;
// 	}
// 	if (checkDoubleInputs(doubleInputs) == false) {
// 		valid = false;
// 	}
// 	if (checkDateInputs(dateInputs) == false) {
// 		valid = false;
// 	}
// 	if (checkTimeInputs(timeInputs) == false) {
// 		valid = false;
// 	}

	// SPECIFIC VALIDATE CODE HERE		

	return valid;
}
function prepare()
{
	if(isInQuotation()==false)
	{
		toggleInQuotationFields();	
	}
	if(isIsGroup()==true)
	{
		toggleIsGroupFields();
	}
	
}



</script>
</head>
<body onload="prepare();">
	<div class="divContainer">
		<jsp:include page="menu.jsp"></jsp:include>
				<form id="inputForm" action="quotationItem?mode=update" method="post" onSubmit="return checkInputs()" onreset="return confirm('Do you really want to reset the form?')">
					<table id="tblInputFields" class="tblInputSmall">
						<tr>
							<td colspan="1">
								<b>Project: <a id="link_project" name="link_project" href="<%=projectUrl %>"><%=projectName %></a></b>
								<br />&gt;
								<b>Quotation: <a id="link_quotation" name="link_quotation" href="<%=quotationUrl %>"><%=quotationName %></a></b>
							</td>
						</tr>
						<tr >
							<td class="inputHeader">EDIT QUOTATION ITEM</td>
						</tr>
						<tr>
					<td id="tdContainer" style="padding-left: 250px; padding-right: 250px;">
						<table cellspacing="0px">
						<tr>
						<td>
							<table id="tblId">
								<tr>
									<td class="tdFieldName">Id</td>
									<td class="tdFieldInput">
										<input class="inputInt readonly" type="text" id="id" name="id" readonly="readonly" value="<%=obj.getIdToString()%>" title="Id of this quotation item. It's readonly."/>
									</td>
									<td class="tdErrMsg">
									</td>
								</tr>
							</table>
						</td>
						<td rowspan="10" style="width: 30px;"></td>
						<td>
							<table id="tblEstimateQuantity">
								<tr>
									<td class="tdFieldNameSmall">Estimate Quantity</td>
									<td class="tdFieldInputSmall">
										<input class="inputInt" type="text" title="Quantity in estimate.<br/>Note: Only integer allowed." id="estimate_quantity" name="estimate_quantity" maxlength="10" value="<%=obj.getEstimateQuantityToString()%>" tabindex="9"  onblur="validateRequiredInput('estimate_quantity');"/>
									</td>
									<td class="tdErrMsg">
										<span id="estimate_quantity_required" class="errMsg" style="display: none;" title="Esitimate quantity is a required field.">(!)</span> 
										<span id="estimate_quantity_invalid" class="errMsg" style="display: none;" title="You've entered an invalid">(!)</span> 
										<span id="estimate_quantity_error" class="errMsg" style="display: none;"><%=obj.errMsgs.EstimateQuantity%></span>
									</td>	
								</tr>
							</table>
						</td>						
						</tr>
						<tr>
						<td>
							<table id="tblParent">
								<tr>
									<td class="tdFieldName">Parent</td>
									<td class="tdFieldInput">
										<select id="id_parent" name="id_parent" class="hyjack">
											<%=HtmlUtil.<QuotationItemModel> genModelSelect( CacheUtil.getIsGroupQuotationItemList(obj.getIdQuotation()), null, obj.getIdParent(), false, true)%>
										</select> 
									</td>
									<td class="tdErrMsg">
									</td>
								</tr>
							</table>
						</td>	
						<td>
							<table id="tblEstimateUnitCost">
								<tr>	
									<td class="tdFieldNameSmall">Estimate Unit Cost</td>
									<td class="tdFieldInputSmall">
										<input class="inputNumeric" title="Unit cost in estimate" type="text" id="estimate_unit_cost" name="estimate_unit_cost" maxlength="20" value="<%=obj.getEstimateUnitCostToString()%>" tabindex="10"  onblur="validateRequiredInput('estimate_unit_cost');"/>
									</td>
									<td class="tdErrMsg">
										<span id="estimate_unit_cost_required" class="errMsg" style="display: none;" title="Unit cost is a required field.">(!)</span> 
										<span id="estimate_unit_cost_invalid" class="errMsg" style="display: none;" title="You've entered an invalid unit cost.">(!)</span> 
										<span id="estimate_unit_cost_error" class="errMsg"><%=obj.errMsgs.EstimateUnitCost%></span>
									</td>
								</tr>
							</table>
						</td>	
						</tr>
								
						<tr>
						<td>
							<table id="tblDisplayIndex">
								<tr>
									<td class="tdFieldName">Display Index</td>
									<td class="tdFieldInput">
										<input class="inputText" title="This looks like bullet and numbering in Word." type="text" id="display_index" name="display_index" maxlength="10" value="<%=obj.getDisplayIndexToString()%>" tabindex="1"  onblur="validateRequiredInput('display_index');"/>
									</td>
									<td class="tdErrMsg">
										<span id="display_index_required" class="errMsg" style="display: none;" title="Display index is a required field. Value must be entered to it.">(!)</span> 
										<span id="display_index_invalid" class="errMsg" style="display: none;" title="You've entered an invalid display index."></span> 
										<span id="display_index_error" class="errMsg"><%=obj.errMsgs.DisplayIndex%></span>
									</td>
								</tr>
							</table>
						</td>	
						<td>
							<table id="tblEstimateTotalAmount">
								<tr>
									<td class="tdFieldNameSmall">Estimate Total Amount</td>
									<td class="tdFieldInputSmall"><input class="inputNumeric" title="Total amount in estimate.<br/>Note: Only numeric allowed" type="text" id="estimate_total_amount" name="estimate_total_amount" maxlength="20" value="<%=obj.getEstimateTotalAmountToString()%>" tabindex="11"  onblur="validateRequiredInput('estimate_total_amount');"/></td>
									<td class="tdErrMsg">
										<span id="estimate_total_amount_required" class="errMsg" style="display: none;" title="Estimate total amount is a required field.">(!)</span> 
										<span id="estimate_total_amount_invalid" class="errMsg" style="display: none;" title="You've enter an invalid value.">(!)</span> <span id="estimate_total_amount_error" class="errMsg"><%=obj.errMsgs.EstimateTotalAmount%></span>
									</td>
								</tr>
							</table>
						</td>	
						</tr>
						<tr>
						<td>
							<table id="tblName">
								<tr>
									<td class="tdFieldName">Name</td>
									<td class="tdFieldInput">
										<input class="inputText" title="Name of quotaion item" type="text" id="name" name="name" maxlength="255" value="<%=obj.getNameToString()%>" tabindex="2"  onblur="validateRequiredInput('name');"/>
									</td>
									<td class="tdErrMsg">
										<span id="name_required" class="errMsg" style="display: none;" title="Name is a required field.">(!)</span> 
										<span id="name_invalid" class="errMsg" style="display: none;" title="You've enter an invalid name.">(!)</span> 
										<span id="name_error" class="errMsg" style="display: none;" title="<%=obj.errMsgs.Name%>">(!)</span>
									</td>
								</tr>
							</table>
						</td>	
						<td>
							<table id="tblRealQuantity">
								<tr>
									<td class="tdFieldNameSmall">Real Quantity</td>
									<td class="tdFieldInputSmall">
										<input class="inputInt" type="text" title="Real quantity.<br/>Note: Only integer allowed" id="internal_quantity" name="internal_quantity" maxlength="10" value="<%=obj.getInternalQuantityToString()%>" tabindex="13"  onblur="validateRequiredInput('internal_quantity');"/></td>
									<td class="tdErrMsg">
										<span id="internal_quantity_required" class="errMsg" style="display: none;" title="Real quantity is required field.">(!)</span> 
										<span id="internal_quantity_invalid" class="errMsg" style="display: none;" title="You've entered an invalid value.">(!)</span> 
										<span id="internal_quantity_error" class="errMsg" style="display: none;"  title="<%=obj.errMsgs.InternalQuantity%>">(!)</span>
									</td>
								</tr>
							</table>
						</td>	
						</tr>
					
						<tr>
						<td>
							<table id="tblDescription">
								<tr>
									<td class="tdFieldName">Description</td>
									<td class="tdFieldInput">
										<textarea id="description" title="Description for this quotation item" name="description" rows="5" class="inputTextarea" tabindex="3"><%=obj.getDescription()%></textarea>
									</td>
									<td class="tdErrMsg"></td>
								</tr>
							</table>
						</td>	
						<td>
							<table id="tblRealUnitCost">
								<tr>
									<td class="tdFieldNameSmall">Real Unit Cost</td>
									<td class="tdFieldInputSmall">
										<input class="inputNumeric" title="Real unit cost.<br/>Note: Only numeric allowed" type="text" id="internal_unit_cost" name="internal_unit_cost" maxlength="20" value="<%=obj.getInternalUnitCostToString()%>" tabindex="14"  onblur="validateRequiredInput('internal_unit_cost');"/>
									</td>
									<td class="tdErrMsg">
										<span id="internal_unit_cost_required" class="errMsg" style="display: none;" title="Real unit cost is a required field.">(!)</span> 
										<span id="internal_unit_cost_invalid" class="errMsg" style="display: none;" title="You've entered an invalid unit cost.">(!)</span> 
										<span id="internal_unit_cost_error" class="errMsg"><%=obj.errMsgs.InternalUnitCost%></span>
									</td>
								</tr>
							</table>
						</td>	
						</tr>
					
						<tr>
						<td>
							<table id="tblRemark">
								<tr>
									<td class="tdFieldName">Remark</td>
									<td class="tdFieldInput"><textarea id="remark" name="remark" title="Remark" rows="5" class="inputTextarea" tabindex="3"><%=obj.getRemark()%></textarea></td>
									<td class="tdErrMsg">
									</td>		
								</tr>
							</table>
						</td>	
						<td>
							<table id="tblRealTotalAmount">
								<tr>
									<td class="tdFieldNameSmall">Real Total Amount</td>
									<td class="tdFieldInputSmall">
										<input class="inputNumeric" title="Real total amount.<br/>Note: Only numeric  allowed" type="text" id="internal_total_amount" name="internal_total_amount" maxlength="20" value="<%=obj.getInternalTotalAmountToString()%>" tabindex="15"  onblur="validateRequiredInput('internal_total_amount');"/>
									</td>
									<td class="tdErrMsg">
										<span id="internal_total_amount_required" class="errMsg" style="display: none;" title="Real total amount is a required field.">(!)</span> 
										<span id="internal_total_amount_invalid" class="errMsg" style="display: none;" title="You've entered an invalid value.">(!)</span> 
										<span id="internal_total_amount_error" class="errMsg"  style="display: none;" title="<%=obj.errMsgs.InternalTotalAmount%>">(!)</span>
									</td>
								</tr>
							</table>
						</td>	
						</tr>
						
						<tr>
						<td>
							<table id="tblIsGroup">
								<tr>
									<td class="tdFieldName">Is Group</td>
									<td class="tdFieldInput">
										<input type="checkbox" id="is_group" name="is_group" title="Check this option to mark this quotation item is a group." <%=obj.getIsGroup() == true ? "checked" : ""%> onchange="toggleIsGroupFields();" tabindex="5"/>
									</td>
									<td class="tdErrMsg"></td>
								</tr>
							</table>
						</td>	
						<td>
							<table id="tblInQuotation">
								<tr>
									<td class="tdFieldNameSmall">In Quotation</td>
									<td class="tdFieldInputSmall">
										<input type="checkbox" class="inputCheckbox" title="Check to mark this is in quotation." id="in_quotation" name="in_quotation" <%=obj.getInQuotation() == true ? "checked" : ""%> onchange="toggleInQuotationFields();" tabindex="15"/>
									</td>
									<td class="tdErrMsg"></td>
								</tr>
							</table>
						</td>	
						</tr>
						<tr>
						<td>
							<table id="tblUnit">
								<tr>
									<td class="tdFieldName">Unit</td>
									<td class="tdFieldInput">
										<select id="id_unit" name="id_unit" class="hyjack" tabindex="6">
											<%=HtmlUtil.<UnitModel> genModelSelect(CacheUtil.getUnitList(), null, obj.getIdUnit(), false, true)%>
										</select> 
										<input class="btnAdd" id="addNewIdUnit" type="button" value="" onclick="ajaxAddItem('#id_unit', 'unit', 'name', 'Please enter your new Unit name!');" title="Click to add new unit" tabindex="7"/>
									</td>
									<td class="tdErrMsg"></td>
								</tr>
							</table>
						</td>	
						<td>
							<table id="tblQuotationQuantity">
								<tr>
									<td class="tdFieldNameSmall">Quotation Quantity</td>
									<td class="tdFieldInputSmall">
										<input class="inputInt" type="text" title="Quantity in quotation.<br/>Note: Only integer allowed" id="quotation_quantity" name="quotation_quantity" maxlength="10" value="<%=obj.getQuotationQuantityToString()%>" tabindex="16"  onblur="validateRequiredInput('quotation_quantity');"/>
									</td>
									<td class="tdErrMsg">
										<span id="quotation_quantity_required" class="errMsg" style="display: none;" title="Quotation quantity is an required field.">(!)</span> 
										<span id="quotation_quantity_invalid" class="errMsg" style="display: none;" title="You've enter an invalid quotation quantity.">(!)</span> 
										<span id="quotation_quantity_error" class="errMsg" style="display: none;" title=" <%=obj.errMsgs.QuotationQuantity%>">(!)</span>
									</td>
								</tr>
							</table>
						</td>	
						</tr>
						
				
						<tr>
						<td>
						</td>	
						<td>
							<table id="tblQuotationUnitCost">
								<tr>
									<td class="tdFieldNameSmall">Quotation Unit Cost</td>
									<td class="tdFieldInputSmall">
										<input class="inputNumeric" title="Unit cost in quotation.<br/>.Note: Only numeric allowed" type="text" id="quotation_unit_cost" name="quotation_unit_cost" maxlength="20" value="<%=obj.getQuotationUnitCostToString()%>" tabindex="17"  onblur="validateRequiredInput('quotation_unit_cost');"/>
									</td>
									<td class="tdErrMsg">
										<span id="quotation_unit_cost_required" class="errMsg" style="display: none;" title="Quotation unit cost is a required field.">(!)</span> 
										<span id="quotation_unit_cost_invalid" class="errMsg" style="display: none;" title="you've entered an invalid value.">(!)</span> 
										<span id="quotation_unit_cost_error" class="errMsg" title="<%=obj.errMsgs.QuotationUnitCost%> "></span>
									</td>
								</tr>
							</table>
						</td>	
						</tr>
						<tr>
						<td>
						</td>
						<td>
							<table id="tblQuotationTotalAmount">
								<tr>
									<td class="tdFieldNameSmall">Quotation Total Amount</td>
									<td class="tdFieldInputSmall">
										<input class="inputNumeric" type="text" id="quotation_total_amount" title="Total amount in quotation.<br/>Note: Only numeric allowed" name="quotation_total_amount" maxlength="20" value="<%=obj.getQuotationTotalAmountToString()%>" tabindex="18"  onblur="validateRequiredInput('quotation_total_amount');"/>
									</td>
									<td class="tdErrMsg">
										<span id="quotation_total_amount_required" class="errMsg" style="display: none;" title="Quotation total amount is a required field.">(!)</span> 
										<span id="quotation_total_amount_invalid" class="errMsg" style="display: none;" title="You've entered an invalid value.">(!)</span> 
										<span id="quotation_total_amount_error" class="errMsg" title=" <%=obj.errMsgs.QuotationTotalAmount%>"></span>
									</td>
								</tr>
							</table>
						</td>	
						</tr>
						<tr class="trInputBottom">
							<td class="tdInputBottom" colspan="3">
								<!-- To modify the position of btnSubmt, plz take a look at toggleIsGroupFields() -->
								<input class="btnSubmit" type="submit" value="Submit" title="Click to save changes" tabindex="19"/>
							</td>
						</tr>
				</table>
				</td>
			</tr>
			<tr class="trSeparator">
				<td></td>
			</tr>
		</table>
	</form>
	<% 
		//String str =  "quotationItemList.jsp?embedded=true&idQuotation=" + obj.getIdQuotation();
		String str =  "quotation.jsp?embedded=true&id=" + obj.getIdQuotation();
	%>
	<jsp:include page="<%=str%>"></jsp:include>
	</div>
</body>
</html>